# Debman completion

have debman && {
have grep-status && {
_comp_dpkg_hold_packages()
{
    grep-status -P -e "^$1" -a -FStatus 'hold' -n -s Package
}
} || {
_comp_dpkg_hold_packages()
{
    grep -B 2 'hold' /var/lib/dpkg/status | grep "Package: $1" | cut -d\  -f2
}
}

_debman()
{
    local cur dashoptions prev special i

    COMPREPLY=()
    cur=`_get_cword`
    prev=${COMP_WORDS[COMP_CWORD-1]}



    for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
        if [[ ${COMP_WORDS[i]} == @(-S|-Sy|-Ss|-Sb|-R|-Rd|-Ql|-Qo|-Qyo|-Qyl|-Qs|-Suy|-Ssy|-Sys|-Si|-Syi|-Siy|-U|-Up|-Sr|-Sw) ]]; then
        special=${COMP_WORDS[i]}
        fi
        #exclude some mutually exclusive options
    done

    if [[ -n "$special" ]]; then
       case $special in
           @(-S|-Si|-Ss|-Sb|-Sy|-Ql|-Qo|-Qyo|-Qyl|-Sys|-Ssy|-Sr|-Sw|-Qi))
               COMPREPLY=( $( apt-cache pkgnames $cur 2> /dev/null ) )
               return 0
               ;;
           @(-R|-Rd|-Qs))
               COMPREPLY=( $( _comp_dpkg_installed_packages "$cur" ) )
               return 0
               ;;
       esac
    fi



    return 0
}
complete -F _debman $default debman
}

# Local variables:
# mode: shell-script
# sh-basic-offset: 4
# sh-indent-comment: t
# indent-tabs-mode: nil
# End:
# ex: ts=4 sw=4 et filetype=sh
